package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ExportProgress;
import com.ruoyi.system.service.impl.ExportProgressService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TEquipment;
import com.ruoyi.system.service.ITEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2025-02-13
 */
@RestController
@RequestMapping("/system/equipment")
public class TEquipmentController extends BaseController
{
    @Autowired
    private ITEquipmentService tEquipmentService;
    @Autowired
    ExportProgressService exportProgressService;

    /**
     * 设备列表
     * 设备列表-徐昭波-20250213
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEquipment tEquipment)
    {
        startPage();
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     *
     */

    //20250213-云计算-智能车联-01-设备管理
    @PreAuthorize("@ss.hasPermi('system:equipment:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public String export(HttpServletResponse response,  TEquipment tEquipment)
    {
        String progressId = exportProgressService.startExport(tEquipment);
        return progressId;
    }
    //20250213-云计算-智能车联-01-设备管理
    @Async
    public Future<Void> performExport(HttpServletResponse response, TEquipment tEquipment, String progressId) {
        try {
            List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
            ExcelUtil<TEquipment> util = new ExcelUtil<>(TEquipment.class);
            util.exportExcel(response, list, "设备数据");
            exportProgressService.completeExport(progressId);
        } catch (Exception e) {
            exportProgressService.failExport(progressId, e.getMessage());
        }
        return new AsyncResult<>(null);
    }
    //20250213-云计算-智能车联-01-设备管理
    @GetMapping("/export/progress/{progressId}")
    public ExportProgress getExportProgress(@PathVariable String progressId) {
        return exportProgressService.getExportProgress(progressId);
    }
    /**
     * 获取设备详细信息
     */
    //20250213-云计算-智能车联-01-设备管理
    @PreAuthorize("@ss.hasPermi('system:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tEquipmentService.selectTEquipmentById(id));
    }

    /**
     * 新增设备
     */
    //20250213-云计算-智能车联-01-设备管理
    @PreAuthorize("@ss.hasPermi('system:equipment:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEquipment tEquipment)
    {
        return toAjax(tEquipmentService.insertTEquipment(tEquipment));
    }

    /**
     * 修改设备
     */
    //20250213-云计算-智能车联-01-设备管理
    @PreAuthorize("@ss.hasPermi('system:equipment:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEquipment tEquipment)
    {
        return toAjax(tEquipmentService.updateTEquipment(tEquipment));
    }

    /**
     * 删除设备
     */
    //20250213-云计算-智能车联-01-设备管理
    @PreAuthorize("@ss.hasPermi('system:equipment:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tEquipmentService.deleteTEquipmentByIds(ids));
    }
}
